/**
 * 数据统计管理初始化
 */
var TestQuestions = {
    id: "TestQuestionsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TestQuestions.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            field: '序号',
            title: 'Number',
            formatter: function (value, row, index) {
                return index + 1;
            }
        },
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', hidden: true},
        {title: '考题', field: 'testTitle', align: 'center', valign: 'middle',},
        {title: '题目类型', field: 'titleType', align: 'center', valign: 'middle',},
        {title: 'A', field: 'optionsA', align: 'center', valign: 'middle',},
        {title: 'B', field: 'optionsB', align: 'center', valign: 'middle',},
        {title: 'C', field: 'optionsC', align: 'center', valign: 'middle',},
        {title: 'D', field: 'optionsD', align: 'center', valign: 'middle',},
        {title: 'E', field: 'optionsE', align: 'center', valign: 'middle',},
        {title: 'F', field: 'optionsF', align: 'center', valign: 'middle',},
        {title: ' 正确答案', field: 'correctAnswer', align: 'center', valign: 'middle',},
        {title: '备注：', field: 'remark', align: 'center', valign: 'middle'},
    ];
};


/**
 * 检查是否选中
 */
TestQuestions.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TestQuestions.seItem = selected[0];
        return true;
    }
};

TestQuestions.openimport = function () {
    var index = layer.open({
        type: 2,
        title: '批量上传试题',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/testQuestions/testQuestions_import/'
    });
    this.layerIndex = index;
};
TestQuestions.downLoadExcel = function () {
    location.href = Feng.ctxPath + '/testQuestions/downloadExcel'
};
/**
 * 删除考题
 */
TestQuestions.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/testQuestions/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            TestQuestions.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

/**
 * 提交添加考题
 */
TestQuestions.openAdd = function () {
    var index = layer.open({
        type: 2,
        title: '添加试题',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/testQuestions/testQuestions_add/'
    });
    this.layerIndex = index;
}

/**
 * 提交修改考题
 */
TestQuestions.openChange = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改试题',
            area: ['800px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/testQuestions/testQuestions_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TestQuestions.formParams = function () {
    var queryData = {};
    queryData['testTitle'] = $("#testTitle").val();
    queryData['titleType'] = $("#titleType").val();
    return queryData;
}

/**
 * 查询考试列表
 */
TestQuestions.search = function () {
    TestQuestions.table.refresh({query: TestQuestions.formParams()});
};

TestQuestions.resetSearch = function () {
    $("#testTitle").val("");
    $("#titleType").val("");
    TestQuestions.search();
};

var parseParam = function (param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function (i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};


$(function () {
    $(function () {
        var defaultColunms = TestQuestions.initColumn();
        var table = new BSTable(TestQuestions.id, "/testQuestions/list", defaultColunms);
        table.setPaginationType("server");
        table.setQueryParams(TestQuestions.formParams());
        TestQuestions.table = table.init();
        // LoanProduct.search();
    });

});
